package view;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.SystemColor;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.CompoundBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

/**
 * This Class is a Customer EditTaarifGUI which extends AbstractGUI Object.
 * @author Tal & Yuda
 *
 */
public class EditTaarifGUI  extends AbstractGUI
{
	private JPanel contentPane=null;
	private JTable table=null;
	private JScrollPane sp=null;
	private JLabel wrnMessage=null;
	private ArrayList<JButton> dyncButtons=null;	 //Dynamic Buttons
	private int nButtons;
	
	/**
	 * This is EditTaarifGUI constructor.
	 * 
	 */
	public EditTaarifGUI()
	{
		
		///main Panel
		      contentPane=new JPanel();
		                     
	      	    contentPane.setBorder(new TitledBorder(null, "Edit taarif", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		        contentPane.setLayout(null);
			    contentPane.setOpaque(true);
			    contentPane.add(getWrnMessage());
			    contentPane.add(getBackButton());
			    this.setContentPane(contentPane);					       
			        
			    //Frame
			    this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
			    this.setTitle("All taarifs");
			 
			    this.setLocationRelativeTo(null); //This will center the JFrame in the middle of the screen
		        this.setLocationByPlatform(true);
		        this.setSize(700, 700);	        	       
	  }
	

	
	
	/**
	 * This method initializes Table
	 */
	private JTable getTableObject() {
		if (table == null) {
			    table = new JTable();
		        table.setColumnSelectionAllowed(true);
		        table.setFillsViewportHeight(true);
		        table.setCellSelectionEnabled(true);
		        table.setSurrendersFocusOnKeystroke(true);
		     
		       
		        table.setBounds(0,0, 500-50, 150-165);
		        table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		        table.setFont(table.getFont().deriveFont(11f));
		 	      
		       
		        table.setBackground(SystemColor.activeCaption);
		        table.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		        table.setRowHeight(30);
		}
		return table;
	}
	
	/**
	 * This method initializes JScrollPane to contain for the JTable
	 */
	private JScrollPane getScrollPaneObject()
	{
		
	     sp = new JScrollPane();
		 sp.setViewportBorder(new CompoundBorder());
		 sp.setBounds(50, 83, 398, 261);

		return sp;
	}
	
	
	/**
	 * This method creates warning message if currently is null.
	 * @return JLabel type.
	 */
	private JLabel getWrnMessage() {
		if (wrnMessage == null) {
			wrnMessage = new JLabel();
			wrnMessage.setForeground(Color.RED);
			wrnMessage.setBounds(113, 41, 320, 23);
			wrnMessage.setVisible(false);
		}
		return wrnMessage;
	}
	
	/**
	 * This method initializes Back Button	
	 */
	private JButton getBackButton() {
		if (backButton == null) {
			backButton = new JButton();
			backButton.setBounds(new Rectangle(530, 505, 123, 35));
			backButton.setText("Back to main");
			
		}
		return backButton;
	}
	
	
	
	/**
	 * *********************************Public Methods 
	 */
	
	/**
	 * This method set Table by data that retrieve from db
	 * @param columnNames,data
	 */
	
	public void setTable(String[] columnNames, Object [][] data)
	{
		//dispose any existing tables
		disposeTable();
		
		table = getTableObject();
		//adjust data from db into table 
		DefaultTableModel def=new DefaultTableModel(data,columnNames);
		
		table.setModel(def);
		//update table
		def.fireTableDataChanged();
		
		//change color header
		JTableHeader header = table.getTableHeader();
	      header.setBackground(Color.YELLOW);
		
		
		//create scrollpane with the new Table
		sp = getScrollPaneObject();
		sp.setViewportView(table);		
		getContentPane().add(sp);
		
	}
	
	/**
	 * This method dispose Buttons
	 * @param void
	 */
	
	public void disposeExistingButtons()
	{
		for(int i = 0 ; i < nButtons; i++)
		{
			getContentPane().remove(dyncButtons.get(i));
			getContentPane().validate();
			getContentPane().repaint();
		}	

		
	}
	
	/**
	 * This method dispose table
	 * @param void
	 */
	
	public void disposeTable()
	{
		if(table!=null)
		{
			table.setVisible(false);
			table = null;
			getContentPane().remove(sp);
		}
	}
	
	
	/**
	 * This method sets the table visibility.
	 * @param boolean b visibility status
	 */
	
	public void setTableVisibilty(boolean b)
	{
		table.setVisible(b);
	}

	
	/**
	 * This method sets buttons to Change Taarif window
	 *@param size return from DB -number of Buttons to Create
	 */

	public void setChangeTaarifbuttons(int size)
	{
		//set all  buttons
		int pos=0;
		
		disposeExistingButtons();
		
		nButtons =  size;
	    dyncButtons = new ArrayList<JButton>();
	    for(int i = 0 ; i < nButtons; i++)
	    {
	    	dyncButtons.add(new JButton("open"));
         	dyncButtons.get(i).setBounds(458, 105+pos, 89, 23);
	    	dyncButtons.get(i).setVisible(true);
	        contentPane.add(dyncButtons.get(i));
	    	pos+=30;
	   }

		getContentPane().validate();
		getContentPane().repaint();
	    
	}

	/**
	 * This method display Warning Message.
	 * @param void	 
	 * */
	
	public void displayWrnMessage(String msg){
		wrnMessage.setText(msg);
		wrnMessage.setVisible(true);
	}
	

	/**
	 * This method adds action listener to Add open button.
	 * @param listener
	 */
	public void addButtonListener(ActionListener listener , int buttonNumber)
	{
		dyncButtons.get(buttonNumber).addActionListener(listener);
	}

}